/**
 * Created by Dalyn on 2017/5/8.
 * 缝制辅料
 */
import React from "react";
import {hashHistory} from 'react-router';
import Relay from "react-relay";
import { notification, message,Input, Icon, Popconfirm } from 'antd';
import {Button} from 'fly';
import ReactKey from "../../../../util/ReactKey";
import OtherRequirementsItem from './OtherRequirementsItem';
import GetProOthersDetailMutation from '../mutation/otherRequireMutation/GetProOthersDetailMutation';
import "../css/otherRequirements.css";
import "../../css/productionManagement_common.css";

export default class SewRaw extends React.Component{
    constructor(props) {
        super(props);
        this.state = {
            token:"",
            dataSource:[],
            visible:"",
        };
        /*假数据*/
        this.dataList = [
            {name:"裁剪注意事项"},
            {name:"缝制工艺注意事项"},
            {name:"整烫包装注意事项"},
            {name:"整烫包装注意事项"},
            {name:"整烫包装注意事项"},
        ];
        this.bigNum = [
            "一",
            "二",
            "三",
            "四",
            "五",
        ];
        this.pdId = this.props.pdId;
    }
    componentWillMount(){
        this.getProOthersDetail();
    }
    /*要求数据请求*/
    getProOthersDetail(){
        Relay.Store.commitUpdate(
            new GetProOthersDetailMutation({
                pdId: this.props.pdId,
            }),
            {
                onFailure: () => {
                    message.error('网络请求失败');
                },
                onSuccess: (response) => {
                    let errorCode = response.getProOthersDetail.responseData.errorCode;
                    let value = response.getProOthersDetail.responseData.value;
                    if (errorCode == 0) {
                        let proOthersDetailModelList = response.getProOthersDetail.responseData.proOthersDetailModelList;
                        this.proOthersDetailModelList = proOthersDetailModelList;
                        if(proOthersDetailModelList==null||proOthersDetailModelList.length==0){
                            proOthersDetailModelList.push({
                                othersId:"",
                                pdId:this.pdId,
                                title:"",
                                content:"",
                                fileJSON:"[]",
                                imgJSON:"[]",
                            });
                        }
                        this.setState({
                            dataSource: proOthersDetailModelList,
                        })
                    } else {
                        // message.warning(value)
                        if (errorCode == 500){
                            message.error('服务器错误,请联系系统管理员');
                        }
                    }
                }

            }
        );
    }
    /*添加要求*/
    addRequire(){
        let dataSourceSelf = this.state.dataSource;
        if(dataSourceSelf.length>=5){
            message.warning("最多可以添加到5条");
            return;
        }
        if(dataSourceSelf.length>=1){
            let lastItemId = dataSourceSelf[dataSourceSelf.length-1].othersId;
            if(lastItemId == ""|| lastItemId == null||lastItemId == undefined){
                message.warning("请先保存上一条数据");
                return;
            }
        }
        dataSourceSelf.push({
            othersId:"",
            pdId:this.pdId,
            title:"",
            content:"",
            fileJSON:"[]",
            imgJSON:"[]",
        });
        this.setState({
            dataSource: dataSourceSelf,
        })
    }
    /*删除要求*/
    deleteRequire(index){
        let dataSourceSelf = this.state.dataSource;
        /*for(let i=1;i<dataSourceSelf.length;i++){
            if(i == index){
                dataSourceSelf.splice(i,1)
            }
        }*/
        let data = dataSourceSelf.filter((v,i)=>{
            return i != index
        });
        this.setState({
            dataSource: data,
        })
    }

    /*点击保存-本地当前item添加请求回来的otherId*/
    saveItem(index,othersId){
        let dataSourceSelf = this.state.dataSource;
        dataSourceSelf[index].othersId = othersId;
        this.setState({
            dataSource: dataSourceSelf,
        })
    }
    render(){
        const {dataSource,token} = this.state,
            {}  = this.props;
        return (
            <div className="" >
                <div className="poduction_otherRequ">
                    <div className="poduction_otherRequ_container">
                        {
                            dataSource.map((item,index)=>{
                                return (
                                    <OtherRequirementsItem
                                        item={item}
                                        index={index}
                                        _token={token}
                                        pdId = {this.pdId}
                                        _deleteRequire={this.deleteRequire.bind(this)}
                                        _saveItem={this.saveItem.bind(this)}
                                        key = {ReactKey.key}
                                    />
                                )
                            })
                        }
                        <footer className="packagSteps_footer">
                            <Button  onClick={this.addRequire.bind(this)}> 添加要求 </Button>
                        </footer>
                    </div>
                </div>
            </div>
        )
    }
}